/*
 * @Author: QinJiu
 * @Date: 2022-06-09 16:50:30
 * @LastEditors: Qinjiu
 * @LastEditTime: 2022-07-20 09:53:24
 * @Description: 参考 https://github.com/axios/axios#canceltoken-deprecated
 */

import axios from "axios";

const CancelToken = axios.CancelToken;

/**
 * 生成单一请求
 * @param { function } 请求函数
 * @return { Promise } 返回一个Promise
 */
export default function generateSignalReq(request) {
  let cancel = null;
  return (option = {}) => {
    // 取消上次请求
    cancel && cancel();
    return request({
      ...option,
      // CancelToken -> axios.CancelToken
      cancelToken: new CancelToken(function executor(c) {
        cancel = c;
      }),
    });
  };
}
